package com.medical.admin.cost.service.Impl;

import com.medical.admin.cost.common.dto.*;
import com.medical.admin.cost.entity.*;
import com.medical.admin.cost.mapper.*;
import com.medical.admin.cost.service.TreatmentService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class TreatmentServiceImpl implements TreatmentService {
    @Resource
    private TreatmentMapper treatmentMapper;

    @Override
    public TreatmentInfo researchByIndentId(Long indentId) {
        //根据订单id关联查询
        TreatmentInfo treatmentInfo = treatmentMapper.selectTreatmentInfo(indentId);
        if (ObjectUtils.isEmpty(treatmentInfo)) {
            throw new ServiceException(ResponseCode.TREATMENT_SELECT_ERROR);
        }
        return treatmentInfo;
    }

    @Override
    public Integer addTreatment(TreatmentInfo treatmentInfo) {
        Treatment treatment = new Treatment();
        treatment.setDepartmentId(treatmentInfo.getDepartmentDto().getDepartmentId());
        treatment.setDoctorId(treatmentInfo.getDoctorDto().getDoctorId());
        treatment.setPatientId(treatmentInfo.getPatientMedicineDto().getPatientId());
        int count = treatmentMapper.insert(treatment);
        if (count <= 0) {
            throw new ServiceException(ResponseCode.TREATMENT_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyTreatment(TreatmentInfo treatmentInfo) {
        Treatment treatment = new Treatment();
        treatment.setDepartmentId(treatmentInfo.getDepartmentDto().getDepartmentId());
        treatment.setDoctorId(treatmentInfo.getDoctorDto().getDoctorId());
        treatment.setPatientId(treatmentInfo.getPatientMedicineDto().getPatientId());
        int count = treatmentMapper.updateById(treatment);
        if (count <= 0) {
            throw new ServiceException(ResponseCode.TREATMENT_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeTreatment(Long indentId) {
        Treatment treatment = treatmentMapper.selectByIndentId(indentId);
        if (ObjectUtils.isEmpty(treatment)) {
            throw new ServiceException(ResponseCode.TREATMENT_SELECT_ERROR);
        }
        int count = treatmentMapper.deleteById(treatment);
        if (count <= 0) {
            throw new ServiceException(ResponseCode.TREATMENT_DELETE_ERROR);
        }
        return count;
    }
}
